package com.yc.qunxing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.qunxing.entity.UserCoupons;
import com.yc.qunxing.mapper.UserCouponsMapper;
import com.yc.qunxing.service.IUserCouponsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鹏哥
 * @since 2024-05-23
 */
@Service
public class UserCouponsServiceImpl extends ServiceImpl<UserCouponsMapper, UserCoupons> implements IUserCouponsService {

    @Autowired
    private UserCouponsMapper userCouponsMapper;

    @Override
    public List<UserCoupons> getCouponsByUserId(Integer userId) {
        return userCouponsMapper.selectList(new QueryWrapper<UserCoupons>().eq("user_id", userId));
    }
    @Override
    public boolean updateCouponNum(Integer id) {
        UserCoupons userCoupons = userCouponsMapper.selectById(id);
        if (userCoupons != null && userCoupons.getCouponNum() > 0) {
            userCoupons.setCouponNum(userCoupons.getCouponNum() - 1);
            int rows = userCouponsMapper.updateById(userCoupons);
            return rows > 0;
        }
        return false;
    }

    @Override
    public boolean deleteZeroCouponNumRecords() {
        QueryWrapper<UserCoupons> wrapper = new QueryWrapper<>();
        wrapper.eq("couponNum", 0);
        int deleted = userCouponsMapper.delete(wrapper);
        return deleted > 0;
    }
}
